package com.employee.crud;

import com.employee.crud.entity.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public class EmployeeRepository {
    private final JdbcTemplate jdbcTemplate;

    @Autowired
    public EmployeeRepository(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }


    public List<Employee> findAll() {
        String SQL = "select * from employee";
        List<Employee> employees = jdbcTemplate.query(SQL,new EmployeeMapper());
        return employees;

    }
    public int insert(int id,String name,int age,String gender){
        String SQL = "insert into employee values(?,?,?,?)";
//        Object[] param = {name,age,gender};
        int s = jdbcTemplate.update(SQL,id,name,age,gender);
        return s;
    }



    public int update(String name,int age,String gender,int id) {
        String sql="update employee set name=?,age=?,gender=? where id=?";
       return jdbcTemplate.update(sql,name,age,gender,id);

    }

    public void deleteById(int id) {
        jdbcTemplate.update("delete from employee where id =  ?", id);
    }

}
